//
//  BookDetailViewController.swift
//  BookStore
//
//  Created by JinYuXin on 2022/5/11.
//

import UIKit

class BookDetailViewController: UIViewController {
    
    @IBOutlet weak var booktitle: UILabel!
    
    @IBOutlet weak var author: UILabel!
    
    @IBOutlet weak var bookprice: UILabel!
    
    @IBOutlet weak var bookcode: UILabel!
    
    @IBOutlet weak var bookdescription: UILabel!
    
    @IBOutlet weak var bookimage: UIImageView!
    
    var bookid:String=""
    var user:String=""
    let defaults = UserDefaults.standard
    var userid:String=""
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //UserShopCarTable.initTable()
        getBookDEtail()
        getUserId()
        

        // Do any additional setup after loading the view.
    }
    
    @IBAction func backButtonPressed(_ sender: UIButton) {
        self.dismiss(animated: true, completion: nil)
    }
    
    @IBAction func `return`(_ sender: UIButton) {
        self.dismiss(animated: true, completion: nil)
    }
    func getBookDEtail()
    {
        let queryResult = BookTable.getBookDEtail(bookid)
        let bookobj = queryResult![0] as! [String:String]
        booktitle.text = bookobj["title"]!
        bookprice.text = bookobj["price"]!
        bookcode.text = bookobj["code"]!
        bookdescription.text = bookobj["description"]!
        author.text = bookobj["author"]!
        bookimage.downloadAsyncFrom(url: bookobj["pic"]!)
    }
    
    @IBAction func addToShopCar(_ sender: Any) {
        //把这一条用户的记录添加到购物车表当中
        UserShopCarTable.initTable()
        let onelist = ShopCar(userid: userid, bookid: bookid)
        UserShopCarTable.InsertUserShopCar(usershopcar: onelist)
        UserShopCarTable.GetUserShopCar()
        
        let controller:MainViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "vcMain") as! MainViewController
        UIApplication.shared.windows[0].rootViewController = controller
        
    }
    func getUserId(){
        user = defaults.string(forKey: "userName")!
        let queryResult = UserTable.getUserId(user)
        let userobj = queryResult![0]
        let a = userobj["id"]!
        let b = a as! Int
        userid = String(b)
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
